package cn.jluzh.blog.modules.user.controller;

import cn.jluzh.blog.commons.entity.CommonEntity;
import cn.jluzh.blog.commons.result.Result;
import cn.jluzh.blog.commons.result.ResultCode;
import cn.jluzh.blog.commons.result.ResultFactory;
import cn.jluzh.blog.modules.log.aspect.annotation.AutoLog;
import cn.jluzh.blog.modules.security.eneity.SecurityUser;
import cn.jluzh.blog.modules.security.eneity.SysUserToken;
import cn.jluzh.blog.modules.security.service.impl.SysUserTokenServiceImpl;
import cn.jluzh.blog.modules.user.entity.SysUser;
import cn.jluzh.blog.modules.user.service.impl.UserServiceImpl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("user")
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    SysUserTokenServiceImpl tokenService;

    @GetMapping("/currentUser")
    public Result getCurrentUser(@RequestHeader("Oauth-Token") String token){
        System.out.println("userToken:===========>"+token);
        JSONObject object = new JSONObject();
        if (!StringUtils.isBlank(token)){
            SysUserToken byToken = tokenService.getByToken(token);
            SysUser sysUser = userService.getById(byToken.getUserId());
            object.put("account", sysUser.getAccount());
            object.put("nickname", sysUser.getNickname());
            object.put("avatarName", sysUser.getAvatarName());
            object.put("avatarUrl", sysUser.getAvatarUrl());
            object.put("id", sysUser.getId());
            object.put("admin",sysUser.getAdmin());
        }
        return ResultFactory.buildSuccessResult(object);
    }

    @GetMapping("getUserInfo")
    @ApiOperation("查询当前用户的基本信息")
    public Result getInfo(@RequestHeader("Oauth-Token")String token){
        SysUser user = SecurityUser.getUser();
        return ResultFactory.buildSuccessResult(user);
    }





    @PostMapping("updateUser")
    @ApiOperation("修改用户基本信息")
    @AutoLog("修改用户信息")
    public Result updateUser(
            @RequestHeader("Oauth-Token")String token,
            @RequestBody SysUser sysUser
    ){
        userService.updateById(sysUser);
        return ResultFactory.buildResult(ResultCode.SUCCESS,"修改成功",null);
    }


    @GetMapping("vaildAccount")
    @ApiOperation("校验用户账号")
    public Result test(
            @RequestParam(name="account")String account) {
        String s = userService.validAccount(account);
        if (s == "该账号可以使用") {
            return ResultFactory.buildSuccessResult(true);
        } else {
            return ResultFactory.buildFailResult(s);
        }
    }

    @PostMapping("updatePwd")
    @ApiOperation("修改密码")
    @AutoLog("修改密码")
    public Result updatePwd(
            @RequestHeader("Oauth-Token")String token,
            @RequestBody CommonEntity entity
    ){
        String s = userService.updatePwd(entity.getId(), entity.getOldPass(), entity.getNewPass());
        if (s=="修改密码成功"){
            return ResultFactory.buildResult(ResultCode.SUCCESS,s,null);
        }else{
            return ResultFactory.buildResult(ResultCode.FAIL,s,null);
        }
    }

    @GetMapping("userTotal")
    @ApiOperation("用户数量")
    public Result userTotal(){
        int count = userService.count();
        return ResultFactory.buildSuccessResult(count);
    }

    @GetMapping("getAllUserByName")
    @ApiOperation("查询所有的用户")
    public Result getAllCategoryPage(@RequestParam(name="pageNo",defaultValue="1") Integer pageNo,
                                     @RequestParam(name="pageSize",defaultValue="10") Integer pageSize,
                                     @RequestParam(name="query", required=false) String query){
        PageHelper.startPage(pageNo, pageSize);
        List<SysUser> allUser;
        if(query!=null){
            allUser=userService.getAllUserByName(query);
        }else {
            allUser = userService.list();
        }
        PageInfo<SysUser> pageInfo = new PageInfo<>(allUser);
        return ResultFactory.buildSuccessResult(pageInfo);
    }

    @PostMapping("addUser")
    @ApiOperation("新增用户")
    @AutoLog("新增用户")
    public Result addUser (@RequestBody SysUser sysUser){
        if(sysUser.getEmail().equals("")){
            sysUser.setEmail(null);
        }
        String s = userService.validAccount(sysUser.getAccount());
        String s1 = userService.validEmail(sysUser.getEmail());
        if (s == "该账号已被使用,请重新输入") {
            return ResultFactory.buildFailResult(s);
        }
        if(s1=="该邮箱已被注册,请重新输入"){
            return ResultFactory.buildFailResult(s1);
        }
        boolean b = userService.saveOrUpdate(sysUser);
        return ResultFactory.buildSuccessResult(b);
    }


    @DeleteMapping("deleteUser")
    @ApiOperation("删除用户")
    @AutoLog("删除用户")
    public Result deleteUser(@RequestParam(name = "id") Integer id){
        boolean b = userService.removeById(id);
        return ResultFactory.buildSuccessResult(b);
    }
}
